<?php
import("ORG.Util.Page"); //分页类
import("@.ORG.Url");

class MessageAction extends Action {

    public static $CLASS_MESSAGE = 2;

    //班级留言
    public function index(){
        //左侧最新留言
        $result = commonModel::getLeaveData();
        $message = new Model("tp_leave");   //班级留言表

        //留言
        $count = $message->table("tp_leave l,tp_user t")->where("t.u_id=l.u_id")->order("l.le_date desc")->count();           //总共的条数
        $Page = new Page($count, PAGE_NUMBER);
        $show = $Page->show();     //分页显示输出

        //班级留言
        $listcount = $message->table("tp_leave l,tp_user t")->where("t.u_id=l.u_id")->limit($Page->firstRow.','.$Page->listRows)->order("l.le_date desc")->select();

        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        // 验证删除权限
        $isHasDelete = UserModel::isRoles(UserModel::USER_DELETE);

        $this->assign("isHasDelete", $isHasDelete);
        $this->assign("isHasInsert", $isHasInsert);
        $this->assign("listcount", $listcount);
        $this->assign("result", $result);
        $this->assign("page", $show);
        $this->display();
    }
    //留言页面
    function Mess_add() {
        $this->display();
    }

    //添加留言
    public function addmessage()
    {
        // 验证添加权限
        $isHasInsert = UserModel::isRoles(UserModel::USER_INSERT);

        if (empty($isHasInsert)) {
            $this->error("您没有添加权限，如有问题，请咨询管理员！");
        }

        $message = new Model("leave");
        //数据准备
        $acontent = array(
            "le_content" => $this->_post("m_content"),
            'le_date' => time(),
            "u_id" => session("u_id"),
        );

        $addcontent = $message->data($acontent)->add();

        if ($addcontent)
        {
            $this->success("留言成功！", "__URL__");
        }
        else
        {
            $this->error("留言失败！");
        }
    }

    //删除留言
    public function MessDel() {
        // 验证删除权限
        $isHasDelete = UserModel::isRoles(UserModel::USER_DELETE);

        if (empty($isHasDelete)) {
            $this->error("您没有删除权限，如有问题，请咨询管理员！");
        }

        if ($this->_get('mid'))
        {
            /*
             *使用事务的方式处理删除留言和看留言回复.
             */

            //删除留言
            $leave = M("leave");
            //事务开启
            $leave->startTrans();

            $result = $leave->where("le_id='" . $this->_get('mid') ."'")->delete();
            // echo ($leave->getLastSql());
            //删除留言回复
            $replay = M("replay_zb");
            $result1 = $replay->where("Link_id='" . $this->_get('mid') ."' and zb_type = '" . self::$CLASS_MESSAGE . "'")->delete();

            if ($result)
            {
                //事物提交.
                $success = $leave->commit();

                if ($success)
                {
                    $this->success("删除成功！");
                }
                else
                {
                    $this->error("删除失败！");
                }
            }
            else
            {
                //失败之后回滚.
                $leave->rollback();
                $this->error("删除失败！");
            }
        }
        else
        {
            $this->error("删除失败！");
        }
    }

    //查看留言.
    function Messone(){
        $leave = M("leave");
        $replay = new Model("replay_zb"); //留言表
        //查看留言信息
        $result1 = $leave->table("tp_leave l,tp_user t")->where("t.u_id=l.u_id and le_id='".$this->_get('le_id')."'")->order("l.le_date desc")->limit(1)->select(); 

        if (empty($result1))
        {
            $this->error("没有找到该留言.");
        }

        //留言的回复信息
        $count = $replay->table("tp_replay_zb  r,tp_user t")->where(" r.u_id=t.u_id and zb_type= " . self::$CLASS_MESSAGE . " and Link_id='".$_GET['le_id']."'")->count();// 总共的条数

        $Page = new Page($count, PAGE_NUMBER);
        $show = $Page->show();// 分页显示输出
        $plmess = $replay->table("tp_replay_zb  r,tp_user t")->where(" r.u_id=t.u_id and zb_type= '" . self::$CLASS_MESSAGE . "' and Link_id='".$_GET['le_id']."'")->limit($Page->firstRow.','.$Page->listRows)->order("zb_id desc")->select();

        //上一个
        $lastRecord = LeaveModel::lastSectionRecord($this->_get('le_id'));
        //下一个
        $nextRecord = LeaveModel::nextSectionRecord($this->_get('le_id'));

        // 验证删除权限
        $isHasReply = UserModel::isRoles(UserModel::USER_REPLY);

        $this->assign("isHasReply", $isHasReply);
        $this->assign("lastRecord", $lastRecord);
        $this->assign("lastRecord", $lastRecord);
        $this->assign("nextRecord", $nextRecord);
        $this->assign("plmess", $plmess);
        $this->assign("result1", $result1);
        $this->assign("page", $show);
        $this->assign("count", $count);
        $this->display();
    }

    //评论回复
    function addplhf(){
        // 验证删除权限
        $isHasReply = UserModel::isRoles(UserModel::USER_REPLY);

        if (empty($isHasReply)) {
            $this->error("您没有回复权限，如有问题，请咨询管理员！");
        }

        $replay = M("replay_zb");

        $data = array(
            'zb_content' => $this->_post('content'),
            'u_id' => session('u_id'),
            'zb_date' => date("Y-m-d H:i:s"),
            'Link_id' => $this->_post('le_id'),
            'zb_type' => self::$CLASS_MESSAGE,　
            );
        $arr = $replay->add($data);

        if ($arr) 
        {
            $this->success();
        } 
        else
        {
            $this->error();
        }
    }
}
?>

